import {
  COMMIT_COURSE, DELETE_COURSE, GET_COURSE_INFO, GET_COURSES,
} from '@/graphql/course';
import { DEFAULT_PAGE_SIZE } from '@/utils/constance';
import { TBaseCourse, TCourseQuery, TCoursesQuery } from '@/utils/types';
import { useLazyQuery, useMutation, useQuery } from '@apollo/client';
import { message } from 'antd';

// 查询
export const useCourses = (pageNum = 1, pageSize = DEFAULT_PAGE_SIZE) => {
  const { data, loading, refetch } = useQuery<TCoursesQuery>(GET_COURSES, {
    skip: true, // 跳过第一次请求
    variables: {
      page: {
        pageNum,
        pageSize,
      },
    },
  });
  const refetchHandler = async (params:{
    name?:string;
    pageSize?:number;
    current?:number
  }) => {
    const { data: res, errors } = await refetch({
      name: params.name,
      page: {
        pageNum: params.current || 1,
        pageSize: params.pageSize || DEFAULT_PAGE_SIZE,
      },
    });
    if (errors) {
      return {
        success: false,
      };
    }
    return {
      total: res?.getCourses.page.total,
      data: res?.getCourses.data,
      success: true,
    };
  };
  return {
    data: data?.getCourses.data,
    page: data?.getCourses.page,
    loading,
    refetch: refetchHandler,
  };
};

export const useCourseForSample = () => {
  const [get, { data, loading }] = useLazyQuery<TCoursesQuery>(GET_COURSES);
  const searchHandler = (name:string) => {
    get({
      variables: {
        name,
        page: {
          pageNum: 1,
          pageSize: DEFAULT_PAGE_SIZE,
        },
      },
    });
  };
  return {
    search: searchHandler,
    data: data?.getCourses.data,
    loading,
  };
};

// 详情
export const useGetCourseInfo = () => {
  // useLazyQuery 调用的时候才去请求，不调用不请求
  const [getCourse, { loading }] = useLazyQuery(GET_COURSE_INFO);
  const getCourseInfo = async (id:string) => {
    const res = await getCourse({
      variables: {
        id,
      },
    });
    return res.data.getCourseInfo.data;
  };
  return {
    getCourseInfo,
    loading,
  };
};
// 详情 不lazy状态
export const useGetCourseInfoSimple = (id:string) => {
  const { data, loading, refetch } = useQuery<TCourseQuery>(GET_COURSE_INFO, {
    variables: {
      id,
    },
  });
  return { data: data?.getCourseInfo.data, loading, refetch };
};

// 删除
export const useDeleteCourse = () => {
  const [del] = useMutation(DELETE_COURSE);
  const delHandler = async (id:string, callback:(isReload?: boolean) => void) => {
    const res = await del({
      variables: {
        id,
      },
    });
    if (res.data.deleteCourse.code === 200) {
      callback(true);
      message.success(res.data.deleteCourse.message);
      return;
    }
    message.error(res.data.deleteCourse.message);
  };
  return [delHandler];
};

// 更新
export const useEditCourseInfo = (): [editHandler: Function, loading: boolean] => {
  const [edit, { loading }] = useMutation(COMMIT_COURSE);
  const editHandler = async (
    id:number,
    params:TBaseCourse,
    callback:(isReload: boolean)=> void,
  ) => {
    const res = await edit({
      variables: {
        id,
        params,
      },
    });
    if (res.data.commitCourseInfo.code === 200) {
      message.success(res.data.commitCourseInfo.message);
      callback(true);
      return;
    }
    message.error(res.data.commitCourseInfo.message);
  };
  return [editHandler, loading];
};
